package com.cy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.bfzcommons.mapper.AdministratorRoleMapper;
import com.cy.bfzcommons.mapper.RoleMapper;
import com.cy.bfzcommons.mapper.RoleRightsMapper;
import com.cy.bfzcommons.pojo.AdministratorRole;
import com.cy.bfzcommons.pojo.Role;
import com.cy.bfzcommons.pojo.RoleRight;
import com.cy.bfzcommons.service.RoleService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdministratorRoleMapper administratorRoleMapper;

    @Autowired
    private RoleRightsMapper roleRightsMapper;


    @Override
    public List<Role> findRole(String roleName) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtil.isEmpty(roleName), "role_name", roleName);
        return roleMapper.selectList(queryWrapper);
    }

    @Override
    public Role selectById(Integer roleId) {
     //   Role role = roleMapper.selectById(roleId);
        Role role = roleMapper.selectRoleRights(roleId);
        return role;
    }

    @Override
    public void insertRole(Role role) {
        List<Integer> rightsList = role.getRightsList();
        roleMapper.insert(role);
        if(rightsList!=null && rightsList.size()>0){
            for (Integer rightsId:rightsList){
                RoleRight roleRight = new RoleRight();
                roleRight.setRightsId(rightsId);
                roleRight.setRoleId(role.getRoleId());
                roleRightsMapper.insert(roleRight);
            }
        }

    }


    @Override
    public void updateRole(Role role) {
        List<Integer> RightsId = role.getRightsList();

        //判断是否有传入修改中间表的值 如果有就根据role的id删除中间表的所有role.roleId的数据
        if (RightsId!=null && RightsId.size()>0){
            QueryWrapper<RoleRight> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id", role.getRoleId());
            roleRightsMapper.delete(queryWrapper);
            for (Integer RightId:RightsId){
                RoleRight roleRight = new RoleRight();
                roleRight.setRightsId(RightId);
                roleRight.setRoleId(role.getRoleId());
                roleRightsMapper.insert(roleRight);
            }


        }

        roleMapper.updateById(role);


    }

    @Override
    public List<Role> selectRoleName() {
        QueryWrapper<Role> queryWrapper= new QueryWrapper<>();
        queryWrapper.select("role_id","role_name");
        List<Role> list = roleMapper.selectList(queryWrapper);

        return list;
    }

    @Override
    public void deleteById(Integer roleId) {
        //删除该角色与菜单的关联
        QueryWrapper<RoleRight> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        roleRightsMapper.delete(queryWrapper);

        //删除该角色与管理员的关联
        QueryWrapper<AdministratorRole> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("role_id", roleId);
        administratorRoleMapper.delete(queryWrapper1);

        //删除该角色
        roleMapper.deleteById(roleId);
    }

}
